package com.mt.roll.wms.dispatch.service.impl;


import com.mt.roll.opcua.s7.my.s7connector.enmuc.S7Client;
import com.mt.roll.opcua.s7.my.s7connector.service.S7Service;
import com.mt.roll.wms.dispatch.LocalDisPatchService;
import com.mt.roll.wms.dispatch.constant.QGSConstant;
import com.mt.roll.wms.dispatch.service.MachineHandService;
import com.mt.roll.wms.dispatch.taskPool.MachineHand1Runnable;

import com.mt.roll.wms.dispatch.taskPool.QGSThreadPool;
import com.mt.roll.wms.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.Future;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/16 16:01
 * @Version 1.0
 */
@Service
public class MachineHandServiceImpl implements MachineHandService {

    @Autowired
    private LocalDisPatchService localDisPatchService;

    @Autowired
    private LocationService locationService;

    //1代表机械手监听任务未启动，2代表启动
    HashMap<String,Future<?>> futureList = new HashMap<>();
    private String machineHand1Name = "1";
    private String machineHand2Name = "2";



    @Override
    public Boolean machineHand1Task() {
        Future<?> future = futureList.get(machineHand1Name);
        if(future!=null){
            //isCancelled：判断任务是否被取消，任务结束包括正常执行结束或异常结束，返回true。
            //isDone：是否执行结束，包括正常执行结束或异常结束。结束返回true。
            boolean cancelled = future.isDone();
            if(cancelled==false){
                return false;
            }
        }

        MachineHand1Runnable runnable = new MachineHand1Runnable(localDisPatchService,locationService);
        Future<?> futureTask;
        synchronized (QGSConstant.LOCK){
            futureTask = QGSThreadPool.machineHandExecutor.submit(runnable);
            futureList.put(machineHand1Name,futureTask);
            return true;
        }


    }

//    @Override
//    public Boolean machineHand2Task() {
//        Future<?> future = futureList.get(machineHand1Name);
//        if(future!=null){
//            //isCancelled：判断任务是否被取消，任务结束包括正常执行结束或异常结束，返回true。
//            //isDone：是否执行结束，包括正常执行结束或异常结束。结束返回true。
//            boolean cancelled = future.isDone();
//            if(cancelled==false){
//                return false;
//            }
//        }
//        MachineHand2Runnable runnable = new MachineHand2Runnable(s7Service,localDisPatchService);
//
//        Future<?> futureTask;
//        synchronized (QGSConstant.LOCK){
//            futureTask = QGSThreadPool.machineHandExecutor.submit(runnable);
//            futureList.put(machineHand1Name,futureTask);
//            return true;
//        }
//
//    }


}
